name: Create.Packages
on:
  workflow_call:
  workflow_dispatch:

jobs:
  package:
    name: Create Packages
    runs-on: windows-2022
    
    steps:        
      - name: Checkout
        uses: actions/checkout@93ea575cb5d8a053eaa0ac8fa3b40d7e05a33cc8

      - name: Prepare Artifacts Directory
        shell: powershell
        run: mkdir artefacts

      - name: Download x64 Artifacts
        uses: actions/download-artifact@9782bd6a9848b53b110e712e20e42d89988822b7
        with:
          name: x64
          path: .\artefacts\x64\

      - name: Download x86 Artifacts
        uses: actions/download-artifact@9782bd6a9848b53b110e712e20e42d89988822b7
        with:
          name: x86
          path: .\artefacts\x86\    
        
      - name: Copy Installation Files To Package Folder
        shell: powershell
        run: |
          Copy-Item artefacts\x86\Up2dateSetup32.msi package\tools\
          Copy-Item artefacts\x64\Up2dateSetup64.msi package\tools\
          ls package\tools\
        
      - name: Insall Checksum with choco
        uses: crazy-max/ghaction-chocolatey@b59ffdb6594d2e2c36d9e1174e15ac799dd0a8f1
        with: 
          args: install checksum

      - name: Prepare Install Script
        shell: powershell
        run: |
          $contentVerify=(Get-Content -path tools\VERIFICATION.txt -Raw)
          $checksum=(checksum -t sha256 -f tools\Up2dateSetup32.msi)
          $replaceTo="checksum for Up2dateSetup32.msi = '$checksum'"
          $replacedVerify=$contentVerify -replace 'checksum for Up2dateSetup32.msi = ''(.*)''',$replaceTo
          $checksum=(checksum -t sha256 -f tools\Up2dateSetup64.msi)
          $contentVerify=$replacedVerify
          $replaceTo="checksum for Up2dateSetup64.msi = '$checksum'"
          $replacedVerify=$contentVerify -replace 'checksum for Up2dateSetup64.msi = ''(.*)''',$replaceTo
          echo $replacedVerify | Set-Content -Path tools\VERIFICATION.txt
        working-directory: package\

      - name: Pack Choco NUPKG
        shell: powershell
        run: choco pack
        working-directory: package\

      - name: Check if Package Created
        shell: powershell
        run: |
          Test-Path -Path *.nupkg -PathType Leaf
          ls .\          
          $packageName=((Get-Item *.nupkg)).Name
          echo "ChocoPackage=$packageName" >> $env:GITHUB_ENV
          echo $packageName >> ChocoPackage.txt
        working-directory: package\      

      - name: Upload nupkg
        uses: actions/upload-artifact@83fd05a356d7e2593de66fc9913b3002723633cb
        with:
          name: ${{ env.ChocoPackage }}
          path: package\${{ env.ChocoPackage }}      

      - name: Upload nupkg
        uses: actions/upload-artifact@83fd05a356d7e2593de66fc9913b3002723633cb
        with:
          name: ChocoPackage.txt
          path: package\ChocoPackage.txt
